package com.tedu.springmvc2.controller;

import com.tedu.springmvc2.mapper.UserMapper;
import com.tedu.springmvc2.pojo.dto.UserDTO;
import com.tedu.springmvc2.pojo.dto.UserUpdateDTO;
import com.tedu.springmvc2.pojo.entity.User;
import com.tedu.springmvc2.pojo.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

//@Controller
@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    //@RequestMapping("/users/reg")

    //@ResponseBody
    @RequestMapping("/reg")
    public String reg(UserDTO userDTO){
        System.out.println("开始处理注册");
        System.out.println(userDTO);
        User user=new User();
        BeanUtils.copyProperties(userDTO,user);
        user.setCreated(new Date());
        System.out.println(user);
        userMapper.insert(user);
        return "注册成功";
    }
//    @RequestMapping("/users/update")
@RequestMapping("/update")
   // @ResponseBody
    public String update(UserUpdateDTO userUpdateDTO){
        User user=new User();
        BeanUtils.copyProperties(userUpdateDTO,user);
        System.out.println(user);
        int num = userMapper.updateById(user);
        return num>0?"修改成功":"修改失败";
    }
   // @RequestMapping("/users/delete")
    //@ResponseBody
    @RequestMapping("/delete")
    public String delete(Integer id){
        System.out.println("用户删除操作,id为:"+id);
        int num = userMapper.deleteById(id);

        return  num>0?"删除成功":"删除失败";
    }
   // @RequestMapping("/users/findAll")
    //@ResponseBody
    @RequestMapping("/findAll")
    public List<UserVO> findAll(){
        return userMapper.selectAll();
    }
}
